/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package giormi;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Ling
 */
public class F_addorder {
    private String o_dateposted;
    private String o_qtysold;
    private String o_datedelivered;
    private String o_drdate;
    private String o_drnum;
    private String o_wsnum;
    private String o_wrnum;
    private String o_qtyreturned;
    private String o_notes;
    private int o_idorder_stock;
    private int o_idcustomer;
    private int o_idstock;

    public void setO_idstock(int o_idstock) {
        this.o_idstock = o_idstock;
    }

    public void setO_datedelivered(String o_datedelivered) {
        this.o_datedelivered = o_datedelivered;
    }

    public void setO_dateposted(String o_dateposted) {
        this.o_dateposted = o_dateposted;
    }

    public void setO_drdate(String o_drdate) {
        this.o_drdate = o_drdate;
    }

    public void setO_drnum(String o_drnum) {
        this.o_drnum = o_drnum;
    }

    public void setO_idcustomer(int o_idcustomer) {
        this.o_idcustomer = o_idcustomer;
    }

    public void setO_notes(String o_notes) {
        this.o_notes = o_notes;
    }

    public void setO_idorder_stock(int o_idorder_stock) {
        this.o_idorder_stock = o_idorder_stock;
    }

    public void setO_qtyreturned(String o_qtyreturned) {
        this.o_qtyreturned = o_qtyreturned;
    }

    public void setO_qtysold(String o_qtysold) {
        this.o_qtysold = o_qtysold;
    }

    public void setO_wrnum(String o_wrnum) {
        this.o_wrnum = o_wrnum;
    }

    public void setO_wsnum(String o_wsnum) {
        this.o_wsnum = o_wsnum;
    }

    private Connection conn;
    private String SQLStatement;
    private String URL;
    private Statement stmt;
    public ResultSet rset;

    public void connect(){
        try{
            DBConnection db = new DBConnection();
            conn = DBConnection.get();
            stmt = conn.createStatement();
        }
        catch(Exception e){
            System.out.println(e.getMessage());
            JOptionPane.showMessageDialog(null,e.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }

    public void disconnect(){
        try{
            stmt.close();
            conn.close();
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
            JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }

    public int getidcustomer(String customername){
        int idcustomer = 0;
        connect();
        try {
            SQLStatement = "SELECT idcustomer FROM customer WHERE "
                    + "c_name = '" + customername + "'";
            rset = stmt.executeQuery(SQLStatement);
            if (rset.next()) {
              idcustomer = rset.getInt(1);
              System.out.println(rset.getInt(1));
            }
            rset.close();
            disconnect();
        } catch (SQLException ex) {
            Logger.getLogger(F_addstock.class.getName()).log(Level.SEVERE, null, ex);
        }
        return idcustomer;
    }
    
    public int getidorder(){
        int idorder = 0;
        connect();
        try {
            SQLStatement = "SELECT MAX(idorder) FROM orderr";
            rset = stmt.executeQuery(SQLStatement);
            if (rset.next()) {
              idorder = rset.getInt(1);
              System.out.println(rset.getInt(1));
            }
            rset.close();
            disconnect();
        } catch (SQLException ex) {
            Logger.getLogger(F_addstock.class.getName()).log(Level.SEVERE, null, ex);
        }
        return idorder+1;
    }


    public void addorder(){
        if (o_drdate.length()!=8)
            o_drdate = date.convertDate(o_drdate);
        if (o_dateposted.length()!=8)
            o_dateposted = date.convertDate(o_dateposted);
        if (o_datedelivered.length()!=8)
            o_datedelivered = date.convertDate(o_datedelivered);

        try{
            SQLStatement = "INSERT INTO orderr "+
                   "(o_dateposted, o_qtysold, o_datedelivered, o_drdate, o_drnum, "+
                   "o_wsnum, o_wrnum, o_qtyreturned, o_notes, o_order_stock_id, o_idcustomer) VALUES"+
                   "('"+o_dateposted+"','"+o_qtysold+"','"+o_datedelivered+"','"+o_drdate+"','"+
                   o_drnum+"','"+o_wsnum+"','"+o_wrnum+"','"+o_qtyreturned+"','"+o_notes+"','"+
                   o_idorder_stock+"','"+o_idcustomer+"')";
            stmt.executeUpdate(SQLStatement);
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
            JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }

    public void updatestock(){
        System.out.println("updatestock");
        int rbalance = 0;
        connect();
        try {
            SQLStatement = "SELECT s_balance FROM stock WHERE idstock=" + o_idstock;
            rset = stmt.executeQuery(SQLStatement);
            if (rset.next()) {
                rbalance = rset.getInt(1);
            }

            System.out.println(rbalance +"-"+ Integer.parseInt(o_qtyreturned)+Integer.parseInt(o_qtysold));
            rbalance -= (Integer.parseInt(o_qtyreturned)+Integer.parseInt(o_qtysold));
            System.out.print(rbalance);

            SQLStatement = "UPDATE stock SET s_balance=" + rbalance + " WHERE idstock=" + o_idstock;
            stmt.executeUpdate(SQLStatement);
            disconnect();
        } catch (SQLException ex) {
            Logger.getLogger(F_addorder.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
